package cn.hl.hm.service.impl;

import com.vizhuo.client.business.appmanage.constant.AppPushConstant;
import com.vizhuo.client.business.appmanage.vo.AndroidPushCustomMessage;
import com.vizhuo.client.business.appmanage.vo.IOSPushCustomMessage;
import com.vizhuo.push.umeng.android.AndroidPayload;
import com.vizhuo.push.umeng.android.AndroidPayloadBody;
import com.vizhuo.push.umeng.android.AndroidPolicy;
import com.vizhuo.push.umeng.android.AndroidSendRequest;
import com.vizhuo.push.umeng.base.PushClient;
import com.vizhuo.push.umeng.base.PushConstant;
import com.vizhuo.push.umeng.base.PushResult;
import com.vizhuo.push.umeng.ios.IOSPayload;
import com.vizhuo.push.umeng.ios.IOSPayloadAps;
import com.vizhuo.push.umeng.ios.IOSPolicy;
import com.vizhuo.push.umeng.ios.IOSSendRequest;
import com.vizhuo.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * 推送至 商户 客户端 服务
 * 
 * @author dcq 作者 E-mail: ding_chuanqin@163.com
 * @date 创建时间：2016年7月1日 下午3:10:39
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class UmengAppPushServiceImpl {
    private static Logger logger = LoggerFactory.getLogger(UmengAppPushServiceImpl.class);



    /**
     * 推送信息给android客户端
     * 
     * @param operationType
     *            操作类型
     * @param displayType
     *            推送显示方式
     * @param pushType
     *            推送方式
     * @param pushTitle
     *            推送标题
     * @param pushText
     *            推送文本信息
     * @param token
     *            推送目标的Token
     * @param client
     *            使用的推送Client
     * @param custom
     *            自定义消息内容
     * @throws Exception
     */
//    @Async
    public void push2Android(int operationType, String displayType, String pushType, String pushTitle, String pushText,
            String token, PushClient client, AndroidPushCustomMessage custom) throws Exception {
//        System.out.println("test android push异步执行");
        AndroidSendRequest req = new AndroidSendRequest();
        req.setType(pushType);
        req.setDevice_tokens(token);
        req.setProduction_mode(client.getProductionMode());// "true/false" // 可选
                                       // 正式/测试模式。测试模式下，只会将消息发给测试设备。
        // 设置发货推送过期时间:6个小时
        if (operationType == AppPushConstant.OPERATION_TYPE_GOOD_TO_DRIVER
                || operationType == AppPushConstant.OPERATION_TYPE_GOOD_TO_INFO) {

            AndroidPolicy policy = new AndroidPolicy();
            policy.setExpire_time(DateUtil.date2String(DateUtil.addHour(new Date(), 6)));
            req.setPolicy(policy);
        }
        AndroidPayload payLoad = new AndroidPayload();
        payLoad.setDisplay_type(displayType);
        AndroidPayloadBody body = new AndroidPayloadBody();
        body.setTitle(pushTitle);
        body.setTicker(pushTitle);
        body.setText(pushText);
        custom.setPushTime(DateUtil.date2String(new Date()));
        body.setCustom(custom);
        body.setAfter_open(PushConstant.AFTER_OPEN_GO_CUSTOM);
        payLoad.setBody(body);
        req.setPayload(payLoad);
        Date now = new Date();
        PushResult sendResult = client.send(req);
        logger.info("Android receive :" + sendResult.getReturnJSON());


    }

    /**
     * 推送消息给IOS客户端
     * 
     * @param operationType
     *            操作类型
     * @param pushType
     *            推送方式
     * @param pushText
     *            推送文本内容
     * @param token
     *            推送的设备Token
     * @param client
     *            推送使用的Client
     * @param custom
     * @throws Exception
     */
//    @Async
    public void push2IOS(int operationType, String pushType, String pushText, String token, PushClient client,
            IOSPushCustomMessage custom) throws Exception {
//        System.out.println("test ios push异步执行");
        IOSSendRequest req = new IOSSendRequest();
        req.setType(pushType);
        // 正式为true，调试环境用false
        req.setProduction_mode(client.getProductionMode());
        req.setDevice_tokens(token);
        IOSPolicy iosPolicy = new IOSPolicy();
        // 设置发货推送过期时间:6个小时
        if (operationType == AppPushConstant.OPERATION_TYPE_GOOD_TO_DRIVER
                || operationType == AppPushConstant.OPERATION_TYPE_GOOD_TO_INFO) {

            iosPolicy.setExpire_time(DateUtil.date2String(DateUtil.addHour(new Date(), 6)));
        }
        req.setPolicy(iosPolicy);
        req.setTimestamp(String.valueOf(System.currentTimeMillis()));
        IOSPayload payload = new IOSPayload();
        IOSPayloadAps aps = new IOSPayloadAps();
        payload.setAps(aps);
        custom.setPushTime(DateUtil.date2String(new Date()));
        payload.setCustom(custom);
        aps.setAlert(pushText);
        aps.setSound("default");
        req.setPayload(payload);
        Date now = new Date();
        PushResult sendResult = client.send(req);
        logger.info("IOS erro :" + sendResult.getSysError());
        logger.info("IOS receive :" + sendResult.getReturnJSON());

    }

}
